package com.hulei.studyproject.spel;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 自定义注解拦截切面类
 * @Title: SpelGetParamAop
 * @Description: TODO
 * @author: hulei
 * @date: 2023/8/10 17:05
 * @Version: 1.0
 */
@Aspect
@Slf4j
@Component
public class SpelGetParamAOP {

    @PostConstruct
    public void init() {
        log.info("SpelGetParam init ......");
    }

    /**
     * 拦截加了SpelGetParam注解的方法请求
     *
     * @param joinPoint    切点
     * @param spelGetParam 注解
     * @return Object
     * @throws Throwable 异常
     */
    @Around("@annotation(spelGetParam)")
    public Object beforeInvoce(ProceedingJoinPoint joinPoint, SpelGetParam spelGetParam) throws Throwable {
        Object result;
        // 方法名
        String methodName = joinPoint.getSignature().getName();
        //获取动态参数
        String param = SpELUtil.generateKeyBySpEL(spelGetParam.param(), joinPoint);
        log.info("spel获取动态aop参数: {}", param);
        //此处可根据获取到的参数进行其他逻辑操作
        log.info("执行目标方法: {} ==>>开始......", methodName);
        result = joinPoint.proceed();
        log.info("执行目标方法: {} ==>>结束......", methodName);
        // 返回通知
        log.info("目标方法 " + methodName + " 执行结果 " + result);
        // 后置通知
        log.info("目标方法 " + methodName + " 结束");
        return result;
    }
}
